package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.common.util.Utility;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.VerificacionService;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;
import co.gov.medellin.ssti.sub.web.form.StrutsUploadForm;

public class VerificacionBeneficiarioAction extends Action {
	@Autowired
	VerificacionService verificacionServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		StrutsUploadForm formBean = (StrutsUploadForm) form;
		String borrar = formBean.getBorrar();

		request.setAttribute("tipo", request.getParameter("tipo"));
		String operacion = request.getParameter("operacion");

		if (borrar != null && operacion != null
				&& operacion.equalsIgnoreCase(Utility.OP_BUSCAR)) {
			if (!borrar.equals("")) {
				HttpSession session = request.getSession();
				UserVO user = (UserVO) session.getAttribute("userInfo");

				// request.setCharacterEncoding("utf-8");
				System.getProperty("file.encoding");

				if (borrar.equals("SI")) {
					try {
						int i = verificacionServiceImpl.deleteRecordsOld(user
								.getName());
					} catch (DAOException e) {
						SUBException.logDebugError("", e);
						formBean.setMsg("Error al eliminar los registros anteriormente cargados");
						return mapping.findForward("fail");
					}
				}
				try {
					// StrutsUploadForm myForm = (StrutsUploadForm) form;
					FormFile myFile = formBean.getTheFile();
					byte[] fileData = myFile.getFileData();
					verificacionServiceImpl
							.uploadFile(fileData, user.getName());

				} catch (DAOException e) {
					SUBException.logDebugError("", e);
					formBean.setMsg("Error del archivo .csv. Verifique la estructura del archivo cargado y recuerde no incluir el encabezado.");
					return mapping.findForward("fail");
				}
			} else {
				return mapping.findForward("success");
			}
		} else {
			return mapping.findForward("success");
		}

		return mapping.findForward("verificados");
	}
}
